package org.example.managere.controller.feibiao;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.managere.dto.feibiao.AmAapOutputDto;
import org.example.managere.entity.feibiao.AmAapOutput;
import org.example.managere.service.feibiao.AmAapOutputService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/feibiao/aap/output")
@RequiredArgsConstructor
public class AmAapOutputController {
    private final AmAapOutputService service;

    /**
     * 分页查询
     */
    @GetMapping
    public Page<AmAapOutputDto> list(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "id,desc") String sort) {
        String[] sortArr = sort.split(",");
        Pageable pageable = PageRequest.of(
                page, size, 
                Sort.by(Sort.Direction.fromString(sortArr.length > 1 ? sortArr[1] : "desc"), sortArr[0])
        );
        return service.findAll(pageable);
    }

    /**
     * 条件搜索（支持关键词）
     */
    @GetMapping("/search")
    public Page<AmAapOutputDto> search(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "id,desc") String sort) {
        String[] sortArr = sort.split(",");
        Pageable pageable = PageRequest.of(
                page, size, 
                Sort.by(Sort.Direction.fromString(sortArr.length > 1 ? sortArr[1] : "desc"), sortArr[0])
        );
        return service.search(keyword, pageable);
    }

    /**
     * 根据电机状态筛选
     */
    @GetMapping("/state/{state}")
    public Page<AmAapOutputDto> findByState(
            @PathVariable AmAapOutput.MotorState state,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "anomalyScore,desc") String sort) {
        String[] sortArr = sort.split(",");
        Pageable pageable = PageRequest.of(
                page, size, 
                Sort.by(Sort.Direction.fromString(sortArr.length > 1 ? sortArr[1] : "desc"), sortArr[0])
        );
        return service.findByMotorState(state, pageable);
    }

    /**
     * 查询高风险电机（异常指数高于阈值）
     */
    @GetMapping("/high-risk")
    public Page<AmAapOutputDto> findHighRiskMotors(
            @RequestParam(defaultValue = "0.7") BigDecimal threshold,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return service.findHighRiskMotors(threshold, pageable);
    }

    /**
     * 查询剩余寿命低的电机
     */
    @GetMapping("/low-life")
    public Page<AmAapOutputDto> findLowLifeMotors(
            @RequestParam(defaultValue = "1000") Float threshold,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return service.findByRemainingUsefulLifeLessThan(threshold, pageable);
    }

    /**
     * 根据电机ID查询
     */
    @GetMapping("/motor/{motorId}")
    public List<AmAapOutputDto> findByMotorId(@PathVariable String motorId) {
        return service.findByMotorId(motorId);
    }

    /**
     * 新增
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public AmAapOutputDto create(@Valid @RequestBody AmAapOutputDto dto) {
        return service.save(dto);
    }

    /**
     * 更新
     */
    @PutMapping("/{id}")
    public AmAapOutputDto update(@PathVariable Integer id, @Valid @RequestBody AmAapOutputDto dto) {
        return service.update(id, dto);
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<AmAapOutputDto> detail(@PathVariable Integer id) {
        return service.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable Integer id) {
        service.deleteById(id);
    }
} 